<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class SubjectController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
	}
	
	function index(){
		$trtotal = $this->curl_http(C('news_url').'newsCategorySpe/queryNewsCategorySpeListPage?token='.$_SESSION["TOKEN"].'&curPage=1&pageSize=1');
		$page = $this->page($trtotal['ReturnData']['page']['totalCount'],20);
		$result = $this->curl_http(C('news_url').'newsCategorySpe/queryNewsCategorySpeListPage?token='.$_SESSION["TOKEN"]."&curPage=".$page->Current_page."&pageSize=".$page->Page_size);
		//dump($result['ReturnData']['newsCategoryList']);exit;
		$this->assign("data", $result['ReturnData']['newsCategoryList']);
		$this->assign("page", $page->show('Admin'));
		$this->display();
	}

	function add(){
		$result = $this->curl_http(C('news_url').'newsTag/findNewsTagList?token='.$_SESSION["TOKEN"]);
		$this->assign("tags", $result['ReturnData']['newsTagList']);
		$this->display();
	}

	function add_post(){
		$name = I("param.cateName", "");
		$tags = I("param.tags");
		$ncUrl = I("param.imageUrl", "");
		if (empty($ncUrl)) {
			$this->error("请上传专题图片！");
		} else {
			$ncUrl = "http://{$_SERVER['HTTP_HOST']}" . $ncUrl;
		}
		$tagIds = implode($tags, ',');
		if(empty($tagIds)){
			$this->error("请至少选择一个标签！");
		}
		if(empty($name)){
			return $this->error("标题名称不能为空！");
		}
		//echo C('news_url').'newsCategory/addNewsCategory?token='.$_SESSION["TOKEN"].'&cateName='.$name.'&ncUrl='.$ncUrl.'&tagIds='.$tagIds;
		$result = $this->curl_http(C('news_url').'newsCategorySpe/addNewsCategory?token='.$_SESSION["TOKEN"].'&cateName='.$name.'&ncUrl='.$ncUrl.'&tagIds='.$tagIds);
		if($result['Success']){
			$this->success("添加成功！", U("Admin/Subject/index"));
		}else{
			return $this->error($result['Message']);
		}
	}

	function edit(){
		$cateId = I("param.cateId", "");
		$result = $this->curl_http(C('news_url').'newsTag/findNewsTagList?token='.$_SESSION["TOKEN"].'&cateId='.$cateId);
		$this->assign("tags", $result['ReturnData']['newsTagList']);

		$result = $this->curl_http(C('news_url').'newsCategorySpe/queryNewsCategorySpe?token='.$_SESSION["TOKEN"].'&cateId='.$cateId);
		$this->assign("data", $result['ReturnData']['newsCategory']);

		$this->display();
	}
	
	function edit_post(){
		$name = I("param.cateName", "");
		$tags = I("param.tags");
		$ncUrl = I("param.imageUrl", 0);
		$tagIds = implode($tags, ',');
		if(empty($tagIds)){
			$this->error("请至少选择一个标签！");
		}
		if(empty($name)){
			return $this->error("标题名称不能为空！");
		}
		$result = $this->curl_http(C('news_url').'newsCategorySpe/updateNewsCategory?token='.$_SESSION["TOKEN"].'&cateName='.$name.'&ncUrl='.$ncUrl.'&tagIds='.$tagIds);
		if($result['Success']){
			$this->success("修改成功！", U("Admin/Subject/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
	
	function del(){
		$typeId = I("param.cateId",0);
		$result = $this->curl_http(C('news_url').'newsCategorySpe/delNewsCategory?token='.$_SESSION["TOKEN"].'&cateIds='.$typeId);
		if($result['Success']){
			$this->success("删除成功！", U("Admin/Subject/index"));
		}else{
			return $this->error($result['Message']);
		}
	}

	function add_tag(){
		$this->display();
	}

	function add_tag_post(){
		$name = I("param.cateName", "");
		if(empty($name)){
			return $this->error("标签名称不能为空！");
		}
		//echo C('news_url').'newsCategory/addNewsCategory?token='.$_SESSION["TOKEN"].'&cateName='.$name.'&ncUrl='.$ncUrl.'&tagIds='.$tagIds;
		$result = $this->curl_http(C('news_url').'newsTag/addNewsTag?token='.$_SESSION["TOKEN"].'&tagTitle='.$name.'&ncUrl='.$ncUrl.'&tagIds='.$tagIds);
		if($result['Success']){
			$this->success("添加成功！", U("Admin/Subject/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
}